package java_sql;

import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class Tool {
	private static String driver;
	private static String url;
	private static String useName;
	private static String passWord;

	static {
		Properties pro = new Properties();
		Reader in = null;
		try {
			in = new FileReader("config.properties");
			pro.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		}
		driver = pro.getProperty("driver");
		url = pro.getProperty("url");
		useName = pro.getProperty("useName");
		passWord = pro.getProperty("passWord");
	}

	public static Connection open() {
		try {
			Class.forName(driver);
			return DriverManager.getConnection(url, useName, passWord);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// public static void close(Statement st, Connection con,) {
	// 	if (st != null) {
	// 		try {
	// 			st.close();
	// 		} catch (SQLException e) {
	// 			e.printStackTrace();
	// 		}
	// 	}
	// 	if (con != null) {
	// 		try {
	// 			con.close();
	// 		} catch (SQLException e) {
	// 			e.printStackTrace();
	// 		}
	// 	}
	// }

	public static void close(Statement st, Connection con, ResultSet rs) {//修改关闭方
		close(st);
		close(con);
		close(rs);
	}

	public static void close(Statement st) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(Connection con) {
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}